
*******CREATING POST BONUS MICROCREDIT DATA FOR TABLE 2***********

set more off
clear
clear matrix
capture log close
	
			
//--> TREATMENT PERIOD MIS DATA 
	*********************************************************************************************************************************************************

		//--> FWCO PANEL: Maps each FAs CO portfolio
		
				use "$data/FWCO_panel.dta"
				
		//--> MERGE w/ CO Month Level MIS data		
				
				sort co_code month 
				merge co_code month using "$data/MIS_comthlevel.dta"
				drop _m
				
		//--> KEEP ONLY POSTBONUS MONTHS (11-25 months); month 10 is bonus annoucement month also dropped
					
				keep if month>10
				
		//--> SOME COS are COMANAGED (SCALE THE ABSOLUTE NUMBERS BY COSHARE WEIGHT)	
			
			
				g weighted_amount_issue = coshare_weight*amount_issue
				g weighted_num_active_loan = coshare_weight*num_active_loan         //--> BONUS TIGGER
				g weighted_num_new_loan = coshare_weight*num_new_loan
				g wmiss_pay_maturity_month = coshare_weight*miss_pay_maturity_month
				
				
		//--> CALCULATE PERFORMANCE AT FA MONTH LEVEL
			*****************************************

				local var weighted_num_active_loan weighted_num_new_loan weighted_amount_issue wmiss_pay_maturity_month miss_pay_month miss_pay_20 num_pay
				collapse (sum) `var', by(fw_code month)
				
				//--> CALCULATE REPAYMENT RATES (RATIOS)
				
						g inv_ratio_miss_pay_month = 1 - (miss_pay_month/num_pay)
						g inv_ratio_miss_pay_20 = 1 - (miss_pay_20/num_pay)         //--> BONUS TRIGGER
				
		//--> CALCULATE AVERAGE PERFORMANCE (FA LEVEL)
			*****************************************
 		
				local credit_var weighted_num_active_loan weighted_num_new_loan weighted_amount_issue wmiss_pay_maturity_month inv_ratio_miss_pay_month inv_ratio_miss_pay_20
				collapse (mean) `credit_var', by(fw_code)
				

					//--> CALCULATE CREDIT INDEX
						*****************************************
					
						//--> STANDARDIZE THE SIX CREDIT OUTCOMES (BASED ON CONTROL GROUP)
						
									sort fw_code
									merge fw_code using "$data/FWHR.dta"
									drop _m
									
							foreach m in `credit_var' {
								sum `m' if treat==0, d
								local mean = r(mean)
								local sd = r(sd)
							
								gen std`m' = (`m' - `mean')/`sd'
								}
							
						//--> INVERSE THE SCALE OF NUMBER OF LOANS PAST MATURITY

							foreach x in wmiss_pay_maturity_month {
								replace std`x' = -1*std`x'
								}
								
								egen credit_index = rowmean(stdweighted_num_active_loan stdinv_ratio_miss_pay_20 stdweighted_num_new_loan stdweighted_amount_issue stdinv_ratio_miss_pay_month stdwmiss_pay_maturity_month)
								sum credit_index if treat==0
								local mean = r(mean)
								local sd = r(sd)
								gen stdcredit_index = (credit_index - `mean')/`sd'
				
				
		keep fw_code `credit_var' stdcredit_index
		sort fw_code
		save "$data/credit_fa_vars.dta", replace
